package com.supermap.dataHandling.utils;

import org.apache.commons.io.FileUtils;

import java.io.*;

/**
 * Created by bruce on 18/9/5 -- 11:11
 */
public class CombineDirectory {
    /**
     * @param args
     */
    public static void main(String[] args) {
        String[] sourceDirNames = {"D:\\Result\\etl"};
        String targetDirName = "D:\\Result\\test";
        combineDirectory(sourceDirNames, targetDirName);
    }

    /**
     * 合并多个文件夹到一个文件夹中
     *
     * @param sourceDirNames
     * @param targetDirName
     */
    public static void combineDirectory(String[] sourceDirNames, String targetDirName) {
        if (sourceDirNames == null || sourceDirNames.length == 0) {
            throw new RuntimeException("待合并的文件夹不存在...");
        }
        for (int i = 0; i < sourceDirNames.length; i++) {
            copyDir(sourceDirNames[i], targetDirName);
        }
        System.out.println("合并所有的文件夹完成...");
    }

    /**
     * 拷贝目录,递归的方法
     *
     * @param sourceDirName
     * @param targetDirName
     */
    public static void copyDir(String sourceDirName, String targetDirName) {
        File sourceDir = new File(sourceDirName);
        File targetDir = new File(targetDirName);
        if (sourceDir == null || !sourceDir.exists()) {
            throw new RuntimeException("待拷贝的文件夹不存在..." + sourceDir.getAbsolutePath());
        }

        if (!sourceDir.isDirectory()) {
            throw new RuntimeException("待拷贝的文件不是目录..." + sourceDir.getAbsolutePath());
        }

        if (!targetDir.exists()) {
            targetDir.mkdirs();
        }

        File[] files = sourceDir.listFiles();

        for (int i = 0; files != null && i < files.length; i++) {

            if (files[i].isFile()) {  //复制文件

                try {

                    FileUtils.copyFileToDirectory(files[i], new File(targetDirName));
                    System.out.println(files[i].getName());
                } catch (IOException e) {
                    e.printStackTrace();
                }

            } else if (files[i].isDirectory()) {   //复制目录,递归的方法
                // 复制目录
                String dir1 = sourceDirName + File.separator + files[i].getName();
                String dir2 = targetDirName + File.separator + files[i].getName();
                copyDir(dir1, dir2);
            }
        }

        System.out.println("拷贝文件夹成功..." + sourceDir.getAbsolutePath());
    }

    /**
     * 拷贝目录,递归的方法
     *
     * @param sourceDirName
     * @param targetDirName
     */
    public static void copyFile(String sourceDirName, String targetDirName) {
        try {
            FileUtils.copyFileToDirectory(new File(sourceDirName), new File(targetDirName));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 拷贝单个的文件
     *
     * @param sourceFile 源文件
     * @param targetFile 目标文件
     */
    private static void copyFile(File sourceFile, File targetFile) {
        FileInputStream in = null;
        BufferedInputStream bis = null;
        FileOutputStream out = null;
        BufferedOutputStream bos = null;
        try {
            // 新建文件输入流并对它进行缓冲
            in = new FileInputStream(sourceFile);
            bis = new BufferedInputStream(in);

            // 新建文件输出流并对它进行缓冲
            out = new FileOutputStream(targetFile);
            bos = new BufferedOutputStream(out);

            // 缓冲数组
            byte[] b = new byte[1024 * 5];
            int len;
            while ((len = bis.read(b)) != -1) {
                bos.write(b, 0, len);
            }
            // 刷新此缓冲的输出流
            bos.flush();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bos != null)
                    bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (bis != null)
                    bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
